package com.school.api;

import com.school.mapper.StuInfoMapper;
import com.school.model.*;
import com.school.service.StuInfoService;
import com.school.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2021-04-03 0:37
 */
@RestController
public class UserController {
    @Autowired
    private  StuInfoService stuInfoService;
    @RequestMapping(value = "/user" ,method = RequestMethod.POST)
    public User getUser(@RequestBody QueryDTO queryDTO){
        User user = new User();
        user.setAddr("合肥市包河区");
        user.setAge(18);
        user.setName("张小帅");
        System.out.println(queryDTO.getId());
        return user;
    }

    @RequestMapping(value = "/option" ,method = RequestMethod.GET)
    public List<OptionDTO> getUser(){
        OptionDTO data1 = new OptionDTO(335,"直接访问");
        OptionDTO data2 = new OptionDTO(310,"邮件营销");
        OptionDTO data3 = new OptionDTO(234,"联盟广告");
        OptionDTO data4 = new OptionDTO(135,"视频广告");
        OptionDTO data5 = new OptionDTO(1600,"搜索引擎");
        OptionDTO data6 = new OptionDTO(39,"其他");
        List<OptionDTO> list = new ArrayList<>();
        list.add(data1);
        list.add(data2);
        list.add(data3);
        list.add(data4);
        list.add(data5);
        list.add(data6);
        return list;
    }
    @RequestMapping(value = "/excel" ,method = RequestMethod.POST,produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public String readExcel(@RequestParam(value ="file") MultipartFile file) throws IOException {
        List<List<Object>> lists = null;
        InputStream inputStream =null;
        stuInfoService.deleteTable();
        if (file.getOriginalFilename().contains(".xlsx")){
            lists = ExcelUtils.readXlsxFirstSheet(file.getInputStream());
        }else if (file.getOriginalFilename().contains(".xls")){
            lists=ExcelUtils.readXlsFirstSheet(file.getInputStream());
        }
        //前三行不处理
        for (int i = 3; i <lists.size() -1 ; i++) {
            StuGradeDTO stuGradeDTO = new StuGradeDTO();
            List<StuScoreDTO > list = new ArrayList<>();
            List<Object> objects = lists.get(i);
            StuScoreDTO var1 = new StuScoreDTO();
            StuScoreDTO var2 = new StuScoreDTO();
            StuScoreDTO var3 = new StuScoreDTO();
            StuScoreDTO var4 = new StuScoreDTO();
            stuGradeDTO.setStuNo(objects.get(1).toString());
            stuGradeDTO.setStuName(objects.get(2).toString());
            var1.setStuNo(objects.get(1).toString());
            var1.setScoreType("课堂表现");
            var1.setStuScore((BigDecimal) objects.get(3));
            var2.setStuNo(objects.get(1).toString());
            var2.setScoreType("作业成绩");
            var2.setStuScore((BigDecimal) objects.get(4));
            var3.setStuNo(objects.get(1).toString());
            var3.setScoreType("实验成绩");
            var3.setStuScore((BigDecimal) objects.get(5));
            var4.setStuNo(objects.get(1).toString());
            var4.setScoreType("期末考试成绩");
            Integer score =
                    Integer.parseInt(objects.get(6).toString())+
                    Integer.parseInt(objects.get(7).toString())+
                            Integer.parseInt(objects.get(8).toString())+
                            Integer.parseInt(objects.get(9).toString())+
                            Integer.parseInt(objects.get(10).toString())+
                            Integer.parseInt(objects.get(11).toString())+
                            Integer.parseInt(objects.get(12).toString());
            var4.setStuScore(BigDecimal.valueOf(score));
            list.add(var1);
            list.add(var2);
            list.add(var3);
            list.add(var4);
            stuGradeDTO.setList(list);
            stuInfoService.save(stuGradeDTO);

        }
        if (null != inputStream){
            inputStream.close();
        }

        return "ok";
    }

    @RequestMapping(value = "/getFinalExam" ,method = RequestMethod.POST)
    List<OutPutScore> getFinalExam(){
        return stuInfoService.getFinalExam();
    }
    @RequestMapping(value = "/getPerformance" ,method = RequestMethod.POST)
    List<OutPutScore> getPerformance(){
        return stuInfoService.getPerformance();
    }
    @RequestMapping(value = "/getHomeWork" ,method = RequestMethod.POST)
    List<OutPutScore> getHomeWork(){
        return stuInfoService.getHomeWork();
    }
    @RequestMapping(value = "/getTest" ,method = RequestMethod.POST)
    List<OutPutScore> getTest(){
        return stuInfoService.getTest();
    }
}
